package vn.edu.saigontech.ncms.web.offering;

import java.util.Date;
import java.util.Set;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.teaching.TeachingSwitch;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.TeacherService;
import vn.edu.saigontech.ncms.service.TeachingSwitchService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateTeachingSwitchAction extends AbstractValidator {
	private Long id;
	private Long offeringId;
	private Integer day;
	private Long inplaceTeacherId;
	private String fromDate;
	private String toDate;
	
	@Override
	public String check() {
		if(Checker.isNull(this.offeringId)) {
			this.sendKeyError("validator.required", "class");
		}
		if(Checker.isNull(day)) {
			this.sendKeyError("validator.required", "teacher");
		}
		if(Checker.isNull(inplaceTeacherId)) {
			this.sendKeyError("validator.required", "inplaceTeacher");
		}
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Teacher teacher = offering.getInstructorOfDay(day, null);
		Teacher inplaceTeacher = TeacherService.INSTANCE.findById(this.inplaceTeacherId);
		if(teacher == inplaceTeacher) {
			this.sendKeyError("validator.notSame", "teacher", "inplaceTeacher");
		}
		if(Checker.isEmpty(this.fromDate)) {
			this.sendKeyError("validator.required", "fromDate");
		}
		if(!Checker.isDate(this.fromDate, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "fromDate", "format.date");
		}
		if(Checker.isEmpty(this.toDate)) {
			this.sendKeyError("validator.required", "toDate");
		}
		if(!Checker.isDate(this.toDate, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "toDate", "format.date");
		}
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		if(fromDateDate.after(toDateDate)) {
			this.sendKeyError("validator.greater", "toDate", "fromDate");
		}
		if(!DateUtil.isBetween(fromDateDate, offering.getStartDate(), offering.getEndDate())) {
			String f = DateUtil.toString(offering.getStartDate(), this.i18n("format.date"));
			String t = DateUtil.toString(offering.getEndDate(), this.i18n("format.date"));
			this.sendKeyError("validator.between", "fromDate", f, t);
		}
		if(!DateUtil.isBetween(toDateDate, offering.getStartDate(), offering.getEndDate())) {
			String f = DateUtil.toString(offering.getStartDate(), this.i18n("format.date"));
			String t = DateUtil.toString(offering.getEndDate(), this.i18n("format.date"));
			this.sendKeyError("validator.between", "toDate", f, t);
		}
		TeachingSwitch teachingSwitch = TeachingSwitchService.INSTANCE.findById(id);
		Set<TeachingSwitch> switchs = offering.getTeachingSwitchs();
		for(TeachingSwitch each : switchs) {
			if(teachingSwitch == null) {
				if(each.getMainDay() == day) {
					Date startDate = each.getFromDate();
					Date endDate = each.getToDate();
					String f = DateUtil.toString(startDate, this.i18n("format.date"));
					String t = DateUtil.toString(endDate, this.i18n("format.date"));
					if(DateUtil.isBetween(fromDateDate, startDate, endDate)) {
						this.sendKeyError("validator.notBetween", "fromDate", f, t);
					}
					if(DateUtil.isBetween(toDateDate, startDate, endDate)) {
						this.sendKeyError("validator.notBetween", "toDate", f, t);
					}
				}
			}
		}
		return this.toSuccess();
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public Long getInplaceTeacherId() {
		return inplaceTeacherId;
	}

	public void setInplaceTeacherId(Long inplaceTeacherId) {
		this.inplaceTeacherId = inplaceTeacherId;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}
}
